/**
 * 表示斗地主游戏
 * @type {{}}
 */

/**
 * 表示玩家的类型
 * @param name
 * @constructor
 */
function Player(name){
    this._init(name);
}
Player.prototype = {
    _init: function (name){
        this.name = name;
        this.cards = []
    },
    showCards: function (){
        this.sort();
        console.log(this.name + ":" + this.cards.join(" "));
    },
    sort : function (){

    }
}

/**
 * 表示一副扑克的类型
 * @constructor
 */
function Poker(){
    this._init();
}
Poker.prototype = {
    _init: function (){
        this.cards = [];
        // 初始化54张牌
        var huases = ["♣", "♠", "♥", "♦"];
        var points = ["A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"]

        for (var i = 0; i < huases.length; i++){
            for (var j = 0; j < points.length; j++){
                var card = new Card(huases[i], points[j]);
                this.cards.push(card);
            }
        }
        this.cards.push(new Card("", "大王"))
        this.cards.push(new Card("", "小王"))

        this.shuffle();
    },
    shuffle: function (){
        /*for (var i = 0; i < 100; i++){
            var index1 = randomInt(0, 53);
            var index2 = randomInt(0, 53);
            var temp = this.cards[index1];
            this.cards[index1] = this.cards[index2];
            this.cards[index2] = temp;
        }*/
        this.cards.sort(function (a, b){
            return Math.random() > 0.5 ? 1 : -1
        })

    }
}

/**
 * 表示每张牌
 * @constructor
 */
function Card(huase, point){
    this._init(huase, point);
}
Card.prototype = {
    _init: function (huase, point){
        this.huase = huase;
        this.point = point;
    },
    toString : function (){
        return this.huase + this.point
    }
}

var game = {
    name: "斗地主",
    players: [new Player("吕布"), new Player("貂蝉"), new Player("董卓")],
    poker: new Poker(),
    startGame: function (){
        // 分牌
        this.sendCard();
        // 在控制台显示牌
        this.showAllCard();
    },
    sendCard : function (){
        for(var i = 0; i < 51; i++){
            var card = this.poker.cards[i];
            this.players[i % this.players.length].cards.push(card);
        }
    },
    showAllCard : function (){
        for(var i = 0; i < this.players.length;i++){
            this.players[i].showCards();
        }
    }
}
game.startGame();




/**
 作者:李振超      2017年8月17日 09:35
 返回随机的 [from, to] 之间的整数(包括from，也包括to)
 */
function randomInt(from, to){
    return parseInt(Math.random() * (to - from + 1) + from);
}
